<template>
	<view class="page-container">
		<view class="login-top-bg">
			<uni-title title="欢迎登录" type="h1" color="#fff"></uni-title>
			<view class="sub-title" style="color: aliceblue;">
				欢迎登录智慧校园系统
			</view>
		</view>
		<view class="login-container">
			<uni-title title="用户登录" type="h3" align="center"></uni-title>
			<view class="login-wrap">
				<uni-forms ref="loginform" :rules="rules" :model="formData">
					<uni-forms-item label="用户名" name="username" label-width="80px" label-align="right">
						<uni-easyinput v-model="formData.username" placeholder="请输入用户名">
							<template></template>
						</uni-easyinput>
					</uni-forms-item>
					<uni-forms-item label="密码" name="password" label-align="right" label-width="80px">
						<uni-easyinput v-model="formData.password" type="password" clearable />
					</uni-forms-item>
				</uni-forms>
				<button type="primary" @click="handlSubmitLogin">登录</button>
				<button @click="handlRegisterClick" style="margin-top: 20px;">注册账号</button>
			</view>
			<view style="display: flex;justify-content: center;margin-top: 100px;">
				<view style="width: 100rpx;height: 80px;text-align: center;" @click="handleWxlogin">
					<uni-icons type="weixin" size="40" color="#00aa00"></uni-icons>
					<view>微信</view>
				</view>
				
			</view>
		</view>

	</view>
</template>

<script>
	import {
		mapState
	} from 'pinia'
	import {
		useUserStore
	} from '@/stores/user.js'
import { wxLogin } from '../../common/js/api/public';
import { login } from '../../common/js/api/user';
	export default {
		data() {
			return {
				formData: {
					username: 'test',
					password: 123,
				},
				rules: {
					username: {
						rules: [{
							required: true,
							errorMessage: '请填写用户名',
						}]
					},
					password: {
						rules: [{
							required: true,
							errorMessage: '请填写密码',
						}]
					}

				},
				validateTrigger: 'submit'
			}
		},
		computed: {
			...mapState(useUserStore, ['userInfo'])
		},
		methods: {
			handlSubmitLogin() {
				console.log(process.env.API_URL);
				this.$refs.loginform.validate().then(() => {
					login(this.formData).then(res=>{
						if(res.code === 0 && res.data && res.data.token) {
							uni.setStorageSync("token",res.data.token)
						}
						useUserStore().getUserInfo()
						uni.switchTab({
							url: '/pages/index/index'
						})
					})
				}).catch(err => {
					console.error(err)
				})

			},
			handlRegisterClick(){
				uni.navigateTo({
					url:'/pages/register/register'
				})
			},
			handleWxlogin(){
				// #ifdef MP-WEIXIN
					wx.login().then(res=>{
						console.log(res);
						wxLogin({code:res.code}).then(wxLoginRes=>{
							uni.setStorageSync('token', res.token)
							useUserStore().getUserInfo()
							uni.switchTab({
								url: '/pages/index/index'
							})
						})
					})
				// #endif
				
			}
		}
	}
</script>

<style scoped>
	.page-container {
		padding: 16px;
	}

	.login-container {
		position: relative;
		margin-top: 100px;
		border-radius: 10px;
		padding: 10px;
		background-color: #ffffff;
		z-index: 10;
	}

	.sub-title {
		color: #666666;
		line-height: 2;
	}

	.login-wrap {
		margin-top: 20px;
	}

	.login-top-bg {
		height: 120px;
		top: 0;
		left: 0;
		width: 100vw;
		padding: 16px;
		position: absolute;
		z-index: 1;
		background: url("@/static/bg2.png");
	}
</style>